<?php
/*
 * 使用mysqli类的方法去实现：
 * s_emp表中所有数据展示在表格中
 * 1.获得所有的列名 show columns from s_emp
 * 					array=>$string
 * 							id,name,salary
 * 2.获得所有列对应的数据 select $string from s_emp
 * 						order by id;
 */
	//print_r($_GET);
	header("content-type:text/html;charset=utf-8");
	$db = new mysqli("localhost","root","flower","{$_GET['dbname']}","3306");
	$db->query("set names utf8");
	/*业务处理1*/
	$obj = $db->query("show columns from {$_GET['tbname']}");
	//var_dump($obj);
	$str_colnames = "";
	if($obj){//非常重要
		$two_arr1 = array();
		while($one_arr = $obj->fetch_row()){
			//$one_arr[0]
			//$one_arr['Field']
			$two_arr1[] = $one_arr;
			
			$str_colnames .= $one_arr[0].",";
		}
		//print_r($two_arr1);
		$str_colnames = substr($str_colnames,0,-1);
	}else{
		echo "有错：",$db->error;
	}
	//echo $str_colnames;
	$arr_colnames = explode(",", $str_colnames);
	/*业务处理2*/
	$obj2 = $db->query("select {$str_colnames} from {$_GET['tbname']}");
	if($obj2){
		$two_arr = array();
		while($one_arr = $obj2->fetch_row()){
			$two_arr[] = $one_arr;
		}
		//print_r($two_arr);
	}else{
		echo "有错",$db->error;
	}
	//执行增删改文本框
	$obj3 = $db->query("show create table {$_GET['tbname']}");
	if($obj3){
		$twoarr = array();
		while($onearr = $obj3->fetch_row()){
			$twoarr[] = $onearr;
			//print_r($twoarr); 
		}
		//echo $twoarr[0][1];
		$arr1 = array();
		$arr1 = explode(",", $twoarr[0][1]);
		
		//echo"<hr>";
		//print_r($arr1);
		
	}
	if(isset($_POST['sql'])){
		$obj4 = $db->query("{$_POST['sql']}");
		if($obj4){
			echo"<script>window.location='right.php'</script>";
		}else{
			echo "执行不成功",$db->error;
		}
	}
	
	
	$db->close();
	
?>
<html>
	<style>
		body{
		background-color:#FCB6B8;
		color:white;}
		textarea{
		width:400px;
		height:200px}
		.a1{
		background-color:#E8728A;
		color:white;}
	</style>
	<body>
		<form action="" method="post">
			<h2>对<?php echo $_GET['tbname']?>表的数据进行新增/更改/删除：</h2>
			<textarea name="sql"></textarea>
			<input type="submit" value="执行" class="a1"/>
		</form>
		<h2><?php echo $_GET['tbname']?>表的结构内容为：</h2>
		<table border="1">
			<tr>
				<th>Field</th>
				<th>Type</th>
				<th>NULL</th>
				<th>Key</th>
				<th>Default</th>
				<th>Extra</th>
			</tr>
			<?php foreach($two_arr1 as $v){?>
			<tr>
				<?php foreach($v as $v1){?>
					
				
				<td>
				<?php if(empty($v1)){
					 echo "NULL";
				}else {echo $v1;}?>
				</td>
				<?php }?>
			</tr>
			<?php }?>
		</table>
		<h2><?php echo $_GET['tbname']?>表的内容为：</h2>
		<table border="1">
			<tr>
				<?php foreach($arr_colnames as $v){ ?>
				<th><?php echo $v; ?></th>
				<?php } ?>
			</tr>
			<?php foreach($two_arr as $one_arr){ ?>
			<tr>
				<?php foreach($one_arr as $value){ ?>
				<td><?php if(empty($value)){ echo "NULL";}else{echo $value; }?></td>
				<?php } ?>
			</tr>
			<?php } ?>
		</table>
	</body>
</html>